package pro.softzhang.algo.lc200;

import pro.softzhang.algo.common.TreeNode;

/**
 * 235. 二叉搜索树的最近公共祖先
 * https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/
 */
public class LC235_LowestCommonAncestorOfABinarySearchTree {
    public static void main(String[] args) {

    }

    /**
     *
     */
    static
    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            int x = root.val;

            if(p.val < x && q.val < x) {
                return lowestCommonAncestor(root.left, p, q);
            }

            if (p.val > x && q.val > x) {
                return lowestCommonAncestor(root.right, p, q);
            }

            return root;
        }
    }
}
